package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.domain.ActivityDO;
import com.qk.dto.activity.ActivityDTO;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-10-12 21:04
 */
@Slf4j
@RestController
@RequestMapping("/activities")
public class ActivityController {
    private static final String TYPE = "type";
    @Autowired
    private ActivityService activityService;

    @GetMapping
    public Result listPage(ActivityDTO activityDTO) {
        return Result.success(activityService.listPage(activityDTO));
    }
    @LogAnno
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        activityService.removeById(id);
        return Result.success();
    }
    @LogAnno
    @PostMapping
    public Result save(@RequestBody ActivityDO activityDO) {
        activityDO.setUpdateTime(LocalDateTime.now());
        activityDO.setCreateTime(LocalDateTime.now());
        activityService.save(activityDO);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        ActivityDO activityDO = activityService.getById(id);
        return Result.success(activityDO);
    }
    @LogAnno
    @PutMapping
    public Result update(@RequestBody ActivityDO activityDO) {
        activityDO.setUpdateTime(LocalDateTime.now());
        activityService.updateById(activityDO);
        return Result.success();
    }

    @GetMapping("/type/{type}")
    public Result listByType(@PathVariable Integer type) {
        List<ActivityDO> list = activityService.listByMap(Map.of(TYPE, type));
        return Result.success(list);
    }

    @GetMapping("/list")
    public Result listAll() {
        List<ActivityDO> list = activityService.list();
        return Result.success(list);
    }
}
